package leetcode.editor.cn;

//Java：删除有序数组中的重复项
public class RemoveDuplicatesFromSortedArray {
    public static void main(String[] args) {
        Solution solution = new RemoveDuplicatesFromSortedArray().new Solution();
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int removeDuplicates(int[] nums) {
            if (nums.length == 0) return 0;
            int i = 0; // 慢指针
            for (int j = 1; j < nums.length; j++) { // 快指针
                if (nums[j] != nums[i]) {
                    nums[++i] = nums[j]; // 发现新的唯一元素，移动到慢指针的下一个位置
                }
            }
            return i + 1; // 数组长度应为最后一个唯一元素的索引 + 1
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}